package com.hsytech.dijiebao.home.alldetail;

import java.sql.Date;
import java.text.SimpleDateFormat;

import org.json.JSONObject;

import com.hsytech.dijiebao.R;
import com.hsytech.dijiebao.account.SignInActivity;
import com.hsytech.dijiebao.base.BaseActivity;
import com.hsytech.dijiebao.utils.Constants;
import com.hsytech.dijiebao.utils.SharedPreferencesTools;
import com.hsytech.dijiebao.view.CircleImageView;
import com.squareup.picasso.Picasso;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

import io.rong.imkit.RongIM;

public class TaxiDetailActivity extends BaseActivity {
	private ImageButton top_back_btn;
	private CheckBox bar_right_top_btn;

	private TextView bar_title_tv;
	private LinearLayout chart;
	private String ID;
	private TextView create_datetime1, hotel_cat_name1, contact_name1, contact_phone1, start_time1, check_in_num1,
			content1, area_name1;
	private String contact_name, contact_phone;
	private CircleImageView circleImageView;
	private String uid = "", publisher_name = "";
	private Boolean isCollected = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.taxi_detail);
		initView();
		getTaxiIntentID();
		getHttp();
	}

	private void initView() {
		initTitle();
		initBody();
	}

	private void initBody() {
		circleImageView = (CircleImageView) findViewById(R.id.CircleImageView);
		create_datetime1 = (TextView) findViewById(R.id.create_datetime);
		hotel_cat_name1 = (TextView) findViewById(R.id.hotel_cat_name);
		contact_name1 = (TextView) findViewById(R.id.contact_name);
		contact_phone1 = (TextView) findViewById(R.id.contact_phone);
		start_time1 = (TextView) findViewById(R.id.start_time);

		check_in_num1 = (TextView) findViewById(R.id.check_in_num);
		content1 = (TextView) findViewById(R.id.content);
		area_name1 = (TextView) findViewById(R.id.area_name);
		contact_phone1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// ����һ����ͼ
				Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + contact_phone));
				// ��ʼ��ͼ(������绰)
				if (checkSelfPermission(Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
					// TODO: Consider calling
					//    Activity#requestPermissions
					// here to request the missing permissions, and then overriding
					//   public void onRequestPermissionsResult(int requestCode, String[] permissions,
					//                                          int[] grantResults)
					// to handle the case where the user grants the permission. See the documentation
					// for Activity#requestPermissions for more details.
					return;
				}
				startActivity(intent);

			}
		});
		// ����
		chart = (LinearLayout) findViewById(R.id.chart);
		chart.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (SharedPreferencesTools.getTOKEN(getApplicationContext()) == null) {
					showToast("���ȵ�½");
					Intent intent = new Intent(TaxiDetailActivity.this, SignInActivity.class);
					startActivity(intent);
				} else {
					if (RongIM.getInstance() != null) {
						/**
						 * �������Ľ��档
						 *
						 * @param context
						 *            Ӧ�������ġ�
						 * @param targetUserId
						 *            Ҫ��֮������û� Id��
						 * @param title
						 *            ����ı��⣬��������ֵ����Ĭ����ʾ��֮������û����ơ�
						 */

						if (!uid.equals("") && !publisher_name.equals("")) {

							RongIM.getInstance().startPrivateChat(TaxiDetailActivity.this, uid, publisher_name);
						}

					}
				}
			}
		});
	}

	private void getTaxiIntentID() {
		Intent intent = getIntent();
		if (intent != null) {
			if (intent.getStringExtra("collectTaxi").equals("collectTaxi")) {
				bar_right_top_btn.setVisibility(View.GONE);
				isCollected = true;
			}
			ID = intent.getStringExtra("idTaxiHelp");
			Log.d("mylog", "����ID" + ID);

		}
	}

	private void getHttp() {
//		RequestParams data = new RequestParams();
//		data.put("id", ID);
//		// showToast("" + ID);
//		data.put("token", SharedPreferencesTools.getTOKEN(this));
//
//		HttpUtils.post(Constants.HTTP_TAXI, this, data, new JsonResponseHandler() {
//			@Override
//			public void onSuccess(JSONObject data) {
//				JSONObject result = data.optJSONObject("result");
//
//				JSONObject myData = result.optJSONObject("data");
//
//				String create_datetime = myData.optString("create_datetime");
//
//				String car_cat_name = myData.optString("car_cat_name");
//
//				contact_name = myData.optString("contact_name");
//				contact_phone = myData.optString("contact_phone");
//
//				String start_time = myData.optString("start_time");
//				String end_time = myData.optString("end_time");
//
//				String carry_passenger_num = myData.optString("carry_passenger_num");
//
//				String content = myData.optString("content");
//
//				String area_name = myData.optString("area_name");
//
//				String city_name = myData.optString("city_name");
//
//				String is_collection = myData.optString("is_collection");
//				String head_photo1 = myData.optString("head_photo");
//				publisher_name = myData.optString("publisher_name");
//				String start_place = myData.optString("start_place");
//				String end_place = myData.optString("end_place");
//				Picasso.with(TaxiDetailActivity.this).load("http://djb.dev.hsydev.com/Upload/image_sm/" + head_photo1)
//						.into(circleImageView);
//				uid = myData.optString("uid");
//				if (is_collection.equals("1")) {
//					bar_right_top_btn.setChecked(true);
//				} else {
//					bar_right_top_btn.setChecked(false);
//				}
//				create_datetime1.setText(create_datetime);
//				hotel_cat_name1.setText(car_cat_name);
//				contact_name1.setText(contact_name);
//				contact_phone1.setText(contact_phone);
//
//				// String a = getStringToDate(start_time);
//				// String b = getStringToDate(end_time);
//				start_time1.setText(start_time + " �� " + end_time);
//				check_in_num1.setText(carry_passenger_num);
//				content1.setText(content);
//				area_name1.setText(start_place + " �� " + end_place);
//
//			}
//		});
	}

	public static String getStringToDate(String time) {
		Date Time = new Date(Long.parseLong(time) * 1000);
		SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd");

		// SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy/MM/dd");

		String retStrFormatNowDate = sdFormatter.format(Time).substring(5);
		return retStrFormatNowDate;
	}

	private void postHttp1() {
		// ����
//		RequestParams data = new RequestParams();
//		if (SharedPreferencesTools.getTOKEN(this) != null) {
//			data.put("token", SharedPreferencesTools.getTOKEN(this));
//		} else {
//			showToast("���ȵ�½");
//		}
//		data.put("type", 1);
//		data.put("tid", ID);
//		HttpUtils.post(Constants.HTTP_DEL_MYSELF, this, data, new JsonResponseHandler() {
//			@Override
//			public void onSuccess(JSONObject data) {
//				// bar_right_top_btn.setChecked(false);
//				Toast.makeText(TaxiDetailActivity.this, "ɾ���ɹ�", Toast.LENGTH_SHORT).show();
//			}
//
//			@Override
//			public void onError(String mess) {
//				// TODO Auto-generated method stub
//				super.onError(mess);
//				showToast(mess);
//			}
//
//		});
	}

	private void postHttp() {
//		RequestParams data = new RequestParams();
//		data.put("id", ID);
//		if (SharedPreferencesTools.getTOKEN(this) != null) {
//			data.put("token", SharedPreferencesTools.getTOKEN(this));
//		} else {
//			showToast("���ȵ�½");
//		}
//		HttpUtils.post(Constants.HTTP_COLLECT_TAXI, data, new JsonResponseHandler() {
//			@Override
//			public void onSuccess(JSONObject data) {
//				showToast("���ղ�");
//			}
//
//			@Override
//			public void onError(String mess) {
//				// TODO Auto-generated method stub
//				super.onError(mess);
//				showToast(mess);
//			}
//		});
	}

	private void initTitle() {
		top_back_btn = (ImageButton) findViewById(R.id.top_back_btn);
		top_back_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		bar_right_top_btn = (CheckBox) findViewById(R.id.bar_right_top_btn);
		bar_right_top_btn.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				if (arg1) {
					if (isCollected) {

					} else {

						postHttp();
					}
					// bar_right_top_btn.setChecked(false);
				} else {
					postHttp1();

					// bar_right_top_btn.setChecked(true);
				}
			}
		});
		bar_title_tv = (TextView) findViewById(R.id.bar_title_tv);
		bar_title_tv.setText("�����ҳ���Ϣ");

	}

}
